package org.yray.netty.demo;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.yray.netty.lengthfield.NettyLengthFieldServerHandler;

public class ChannelFixLenServer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        System.out.println ("初始化pipeline");
//编码,将String转码为ByteBuf
        ch.pipeline ().addLast ("encode", new StringEncoder ());
//固定接受10个字节,如果超过这个字节数,超出的部分存在tcp的缓存中,等待下一次传输,超出的部分不会丢弃掉
        ch.pipeline ().addLast (new FixedLengthFrameDecoder(10));
//解码,将ByteBuf解码为String
        ch.pipeline ().addLast ("decode", new StringDecoder ());
//handler一般都是放在最后面,建议一个就可以,如果业务复杂拆分多个,那在handler中调用fireXXX才会向下一个handler继续调用
        ch.pipeline ().addLast (new NettyLengthFieldServerHandler());

    }
}
